package io.netty.example.study.common;

import io.netty.example.study.common.auth.AuthOperation;
import io.netty.example.study.common.auth.AuthOperationResult;
import io.netty.example.study.common.keepalive.KeepAliveOperation;
import io.netty.example.study.common.keepalive.KeepAliveOperationResult;
import io.netty.example.study.common.order.OrderOperation;
import io.netty.example.study.common.order.OrderOperationResult;

public enum OperationType {

    AUTH(1, AuthOperation.class, AuthOperationResult.class),
    KEEPALIVE(2, KeepAliveOperation.class, KeepAliveOperationResult.class),
    ORDER(3, OrderOperation.class, OrderOperationResult.class)
    ;

    private int opCode;
    private Class<? extends Operation> operationClazz;
    private Class<? extends OperationResult> operationResultClazz;

    OperationType(int opCode, Class<? extends Operation> operationClazz, Class<? extends OperationResult> operationResultClazz) {
        this.opCode = opCode;
        this.operationClazz = operationClazz;
        this.operationResultClazz = operationResultClazz;
    }

    public static OperationType fromOpCode(int opcode) {
        if (opcode == AUTH.getOpCode()) {
            return AUTH;
        } else if (opcode == KEEPALIVE.getOpCode()) {
            return KEEPALIVE;
        } else if (opcode == ORDER.getOpCode()) {
            return ORDER;
        } else {
            return null;
        }
    }

    public static OperationType fromOperation(Operation operation) {
        if (operation.getClass() == AuthOperation.class) {
            return AUTH;
        } else if (operation.getClass() == KeepAliveOperation.class) {
            return KEEPALIVE;
        } else if (operation.getClass() == OrderOperation.class) {
            return ORDER;
        } else {
            return null;
        }
    }

    public int getOpCode() {
        return opCode;
    }

    public Class<? extends Operation> getOperationClazz() {
        return operationClazz;
    }

    public Class<? extends OperationResult> getOperationResultClazz() {
        return operationResultClazz;
    }

}
